﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace MTRC.ODMS.Web.SOM.Reports
{
    public partial class FireLinkChangeDetail : BasePage_SOM
    {
        protected override string MenuID
        {
            get
            {
                return "M140305";
            }
        }

        protected string ReportID
        {
            get
            {
                return "SOM001";
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                SetLanguage();
                ucPsc.MenuID = this.MenuID;
                ucPsc.BindUcPsc(this.MenuID, "M140305_F2", "M140305_F3");
            }
        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            this.ChangeLanguage += new ChangeLanguageEventHandler(SetLanguage_Click);
        }
        protected void SetLanguage_Click(object sender, EventArgs e)
        {
            ucPsc.MenuID = this.MenuID;
            ucPsc.BindUcPsc(this.MenuID, "M140305_F2", "M140305_F3");
            SetLanguage();
        }

        private void SetLanguage()
        {
            this.btnPreview.Text = GetLabel("COM_Print");
        }

        /// <summary>
        /// Preview
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnPreview_Click(object sender, EventArgs e)
        {
            // Form Date
            DateTime from = this.ucPsc.GetFromDateFromUcPsc();

            if (string.IsNullOrEmpty(this.FormatDateTime(from)))
            {
                this.PAlert(this.GetMessage("SOM_FormDate"));
                return;
            }

            // To Date
            DateTime to = this.ucPsc.GetToDateFromUcPsc();
            if (string.IsNullOrEmpty(this.FormatDateTime(to)))
            {
                this.PAlert(this.GetMessage("SOM_ToDate"));
                return;
            }
            //Begjin liuwenjing add 2015-09-30 issue:U224
            if (from.ToString().Length > 0 && to.ToString().Length > 0)
            {
                if (InvalidateInputDate(from, to))
                {
                    PAlert(this.GetMessage("COM_StartEndDate"));
                    return;
                }
            }
            //end

            // Station Ids
            string locationIds = this.ucPsc.GetLocationIDFromUcPsc();

            //add by jack 20160127 for ISU27
            //if (this.ddlExport.SelectedValue=="2") //Remove by jack 20160202 isu27 need to validate pdf
            //{
            int rowCount = MTRC.ODMS.BLL.SOM.Event.StationEventFirelink.GetRowsCountInFirelinkExcel(Convert.ToInt32(this.LangCode), from, to, locationIds);
            int excelRowsMaxSize = 0;
            if (!string.IsNullOrEmpty(APJ.Util.Utility.GetAppSetting("SOM_Firelink_ExcelRowsMaxSize")))
            {
                try
                {
                    excelRowsMaxSize = Convert.ToInt32(APJ.Util.Utility.GetAppSetting("SOM_Firelink_ExcelRowsMaxSize"));
                }
                catch (Exception ex)
                {
                    this.PAlert(this.GetMessage(ex.Message));
                    APJ.Util.Log.WindowLog.Log(ex.Message.Replace('"', '\''), APJ.Util.Log.LogType.Error, this.User.Identity.Name);
                }
            }

            if (rowCount > excelRowsMaxSize)
            {
                this.PAlert(this.GetMessage("SOM_FireLinkChangeDetail_ReportRow_Msg"));
                return;
            }
            //}
            //End add by jack 20160127 for ISU27

            MTRC.ODMS.Web.WebComponent.reportbasepage report = new ODMS.Web.WebComponent.reportbasepage();
            report.ReportID = this.ReportID;
            report.PrintFormat = int.Parse(this.ddlExport.SelectedValue);
            report.AddParameter("ReportID", this.ReportID);
            report.AddParameter("LangCode", this.LangCode);
            report.AddParameter("FromDate", this.FormateDateTimeToODBCString(from.ToString()));
            report.AddParameter("ToDate", this.FormateDateTimeToODBCString(to.ToString()));
            report.AddParameter("StationIDs", locationIds);

            string[] returnScript = new string[1];
            report.CreateReport(true,returnScript);
            Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "Export", returnScript[0], true);
        }
    }
}